package com.tengju.bff.interfaces.manage.member.article.assembler;

import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.manage.member.article.vo.ArticleVO;
import com.tengju.bff.interfaces.manage.member.article.vo.PageArticleVO;
import com.tengju.support.domain.model.article.Article;
import com.tengju.user.application.archives.dto.PageQueryDTO;
import org.springframework.util.CollectionUtils;

import java.time.format.DateTimeFormatter;
import java.util.List;

public class ArticleAssembler {

    public static PageArticleVO ModelToVO(List<Article> articleList, PageQueryDTO pageQueryDTO, Integer total) {

        List<ArticleVO> articleVOList = Lists.newArrayList();
        PageArticleVO pageArticleVO = new PageArticleVO();
        pageArticleVO.setPageIndex(pageQueryDTO.getPageIndex());
        pageArticleVO.setPageSize(pageQueryDTO.getPageSize());
        pageArticleVO.setTotal(total);
        if (CollectionUtils.isEmpty(articleList)) {
            return pageArticleVO;
        }
        for (Article article : articleList) {
            ArticleVO articleVO = new ArticleVO();
            articleVO.setArticleId(article.getArticleId().getValue());
            articleVO.setTitle(article.getTitle());
            articleVO.setType(article.getArticleType().getVal());
            articleVO.setContext(article.getContext());
            articleVO.setCreateBy(article.getCreateBy());
            articleVO.setCreateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(article.getCreateTime()));
            articleVOList.add(articleVO);
        }
        pageArticleVO.setArticleVOList(articleVOList);
        return pageArticleVO;
    }
}
